<?php


namespace app\home\controller;


use app\base\BaseController;
use think\facade\Db;
use think\facade\View;

/**
 * Class SfAccount
 * @package app\home\controller
 * 收发明细汇总
 */
class SfAccount extends BaseController
{
    protected $title='收发明细';
    protected $tabel='material_kucun_view';
    public function index()
    {

        $param=$this->param;
        $doDate=isset($param['doDate'])&&$param['doDate']?$param['doDate']:date('Y-m-01').' ~ '.date('Y-m-d');
        if (request()->isAjax()) {
            $se_type=$param['se_type']??1;
            $where[]=['status','=',2];
            $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
            if ($se_type==1){
                $table='ruku_view';
            }else{
                $table='chuku_view';
            }
            foreach (['orderNum','pName','mdTypeName','bar_code','mdName','specs','wName'] as $v){
                if (isset($param[$v])&&$param[$v]){
                    $where[]=[$v,'like','%'.$param[$v].'%'];
                }
            }

                $dates=explode('~',$doDate);
                $where[]=['doDate','between',$dates];
            $sql = Db::name($table)
                ->where($where)
//                ->field('*')
                ->order('doDate Desc')
                ->group('material_dossier_id');
            if ($se_type==1){
                $sql->field('*,sum(inNum) as inNum');
            }else{
                $sql->field('*,sum(sendNum) as sendNum');
            }
            $list=$sql->paginate($rows)->each(function ($item) use ($se_type,$dates,$where){
                    $qichuInNum=Db::name('ruku_view')
                        ->where('status',2)->where('doDate','<',$dates[0])
                        ->where('material_dossier_id',$item['material_dossier_id'])
                        ->sum('inNum');
                    $qichuOutNum=Db::name('chuku_view')
                        ->where('status',2)->where('doDate','<',$dates[0])
                        ->where('material_dossier_id',$item['material_dossier_id'])
                        ->sum('sendNum');
                    $qichu=$qichuInNum-$qichuOutNum;
                    if ($se_type==1){
                        $item['sendNum']=Db::name('chuku_view')
                            ->where($where)
                            ->where('material_dossier_id',$item['material_dossier_id'])
                            ->sum('sendNum');
                    }else{
                        $item['inNum']=Db::name('ruku_view')
                            ->where($where)
                            ->where('material_dossier_id',$item['material_dossier_id'])
                            ->sum('inNum');
                    }
                    $item['qcNum']=$qichu;
                    $item['qmNum']=$qichu+$item['inNum']-$item['sendNum'];

                    return $item;
                });
            return table_assign(0, '', $list);
        } else {
            View::assign('doDate',$doDate);
            return view();
        }

    }

}